package com.diandireader.activity;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.diandireader.activity.R;
import com.diandireader.domain.BookMarkVO;
import com.diandireader.domain.DiandiReaderVO;
import com.diandireader.service.DianDiReaderService;
import com.diandireader.util.ActivityManager;
import com.diandireader.util.ColorPickerDialog;
import com.diandireader.util.FileHelper;
import com.diandireader.util.StringHelper;
import com.diandireader.util.ColorPickerDialog.OnColorChangedListener;

/**
 * 1、show text in the textview,allow be edited 
 * 2、no linespace，text margin，text padding .. 
 * 3、no use scrool view 
 * 4、read one page each time
 * 
 * @author mayh
 * 
 */
public class SimpleTextReaderActivity extends Activity {

	private File file;
	private DiandiReaderEditText readerEditText;
	private TextView pageCount, bookName, fontColor, font_text_view;
	private Handler handler;
	private SharedPreferences preferences;
	private SharedPreferences readPositionPreference;
	private long totalSkipChars;// 跳过的字符数
	private float fontSize;// 设置字体的大小
	private DisplayMetrics displayMetrics;
	private int screenWidth;// 屏幕宽度
	private int screenHeight;// 屏幕高度
	private int countInColumns;// 每列有几个字符
	private int countInRows;// 每行的字符数
	private int maxPageBytes;// 每页的最大数量
	private int currentPageChars;// 每页可以显示字符数
	private InputStreamReader reader;
	private String codeType;
	private float positionX;
	private long readResult, fileSize;
	private boolean longPress = false;// 是否是长按
	private boolean moving = false;// 是否是滑动
	private DecimalFormat format;
	private View readerOptionView;
	private GridView gridView;
	private boolean isScrolling;
	private int tempProgress;
	private int textColor, tempColor;
	List<BookMarkVO> bookmarkList;
	private RelativeLayout bookInfoLayout;
	private RelativeLayout optionMenu;
	private BookMarkVO bm;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 去掉标题栏
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//      去掉状态栏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.reader);
		ActivityManager.add(this);
		Intent intent = getIntent();
		String filePath = intent.getStringExtra("filePath");
		file = new File(filePath);
		format = new DecimalFormat("0.00");
		preferences = getSharedPreferences(DiandiReaderVO.SETTING,
				Context.MODE_PRIVATE);
		readPositionPreference = getSharedPreferences(
				DiandiReaderVO.READPOSITION, Context.MODE_PRIVATE);
		handler = new OptionHandler();
		setViews();
		setListeners();
		fileSize = readPositionPreference.getLong("T"+file.getName(), 0);
		//calculate the filesize when the first time open this file
		if(fileSize==0){
		FileHelper.IndexCreator indexCreateThread = new FileHelper.IndexCreator(SimpleTextReaderActivity.this,filePath);
		indexCreateThread.start();
		}
	}

	// 设置View
	public void setViews() {
		readerEditText = (DiandiReaderEditText) findViewById(R.id.readerEditText);
		pageCount = (TextView) findViewById(R.id.pagecounts);
		bookName = (TextView) findViewById(R.id.bookname);
		readerOptionView = ViewFactory.getReadOptionMenu(this);
		gridView = (GridView) readerOptionView
				.findViewById(R.id.reader_option_grid);
		bookInfoLayout = (RelativeLayout) findViewById(R.id.bookinfo);
		optionMenu = (RelativeLayout) findViewById(R.id.optionMenu);
		optionMenu.addView(readerOptionView);
	}

	// 设置Listener
	public void setListeners() {
		if(preferences.getInt(DiandiReaderVO.BACKGROUNDIMAGE, DiandiReaderVO.BACKGROUNDIMAGE_DAY)==DiandiReaderVO.BACKGROUNDIMAGE_DAY){
			readerEditText.setBackgroundResource(R.drawable.reader_main_background_day);
		}else {
			readerEditText.setBackgroundResource(R.drawable.reader_main_background);
		}
		readerEditText.post(new Runnable() {
			@Override
			public void run() {
				// load user config
				loadUserCfg();
				initText();
			}
		});
		readerEditText.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {

				if (optionMenu.getVisibility() == View.VISIBLE)
					optionMenu.setVisibility(View.GONE);

				// 按下
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					longPress = false;
					moving = false;
				}
				// 移动
				if (event.getAction() == MotionEvent.ACTION_MOVE) {
					moving = true;
				}
				// 抬起
				if (event.getAction() == MotionEvent.ACTION_POINTER_UP) {

				}
				positionX = event.getX();
				return false;
			}
		});
		readerEditText.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// if event is long press or touch moving ,return
				if (moving || longPress)
					return;
				// if press region less than 1/3 width
				if (positionX < screenWidth / 3) {
					setPrevPageText();
				}
				// if press region greater than 2/3 width
				if (positionX > screenWidth * 2 / 3) {
					setNextPageText();
				}
			}
		});
		readerEditText.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
				longPress = true;
				return false;
			}
		});
		gridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long arg3) {

				optionMenu.setVisibility(View.GONE);

				switch (position) {
				case 0: {
					TextView text = (TextView) view
							.findViewById(R.id.reader_option_item_text);
					if (!isScrolling) {
						text.setText("停止翻页");
						handler
								.sendEmptyMessage(DiandiReaderVO.MESSAGE_SCROLLING);
						isScrolling = true;
					} else {
						text.setText("自动翻页");
						handler
								.sendEmptyMessage(DiandiReaderVO.MESSAGE_SCROLLSTOP);
						isScrolling = false;
					}
				}
					break;
				case 1: {
					// add book mark
					final String text = readerEditText.getText().toString();
					new Thread(new Runnable() {
						@Override
						public void run() {
							DianDiReaderService ds = new DianDiReaderService();
							ds.saveBookmark(file.getName(), StringHelper.subString(text, 15), totalSkipChars);
						}
					}).start();
					Toast.makeText(SimpleTextReaderActivity.this, "书签添加成功",
							Toast.LENGTH_SHORT).show();
				}
					break;
				case 2: {
					// jump
					AlertDialog.Builder builder = new AlertDialog.Builder(
							SimpleTextReaderActivity.this);
					AlertDialog.Builder jumpDialog = new AlertDialog.Builder(
							SimpleTextReaderActivity.this);
					final View jumpView = View.inflate(
							SimpleTextReaderActivity.this,
							R.layout.reader_jump, null);
					SeekBar seekBar = (SeekBar) jumpView
							.findViewById(R.id.jump_seekbar);
					final TextView textView = (TextView) jumpView
							.findViewById(R.id.seekbar_text);
					final long totalChars = readPositionPreference.getLong("T"+file.getName(),0);
					if(totalChars==0)
						return;
					textView.setText(format.format((double) (totalSkipChars + currentPageChars)
							* 100 / (double) totalChars)
							+ "%");
					final long temTotalSkipChars = totalSkipChars;
					seekBar.setMax((int)totalChars);
					seekBar.setProgress((int)(totalSkipChars + currentPageChars));
					seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
								@Override
								public void onStopTrackingTouch(SeekBar seekBar) {
								}

								@Override
								public void onStartTrackingTouch(SeekBar seekBar) {
								}

								@Override
								public void onProgressChanged(SeekBar seekBar,
										int progress, boolean arg2) {
									totalSkipChars = progress;
									textView.setText(format.format((double) totalSkipChars
													* 100 / (double) totalChars)
											+ "%");
								}
							});
					jumpDialog.setTitle("跳转");
					jumpDialog.setView(jumpView);
					jumpDialog.setPositiveButton("确定", new  DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							initText();
						}
					});
					jumpDialog.setNegativeButton("取消",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface arg0,
								int arg1) {
							totalSkipChars = temTotalSkipChars;
							initText();
						}
					});
					jumpDialog.show();
				}
					break;
				case 3: {
					// text style
					AlertDialog.Builder fontDialog = new AlertDialog.Builder(
							SimpleTextReaderActivity.this);
					View fontView = View.inflate(SimpleTextReaderActivity.this,
							R.layout.reader_font, null);
					SeekBar seekBar = (SeekBar) fontView
							.findViewById(R.id.font_seekbar);
					fontColor = (TextView) fontView
							.findViewById(R.id.font_color);
					font_text_view = (TextView) fontView
							.findViewById(R.id.font_text_view);
					tempColor = textColor;
					seekBar.setProgress((int) fontSize);
					tempProgress = seekBar.getProgress();
					font_text_view.setTextSize(fontSize);
					fontColor.setTextColor(textColor);
					seekBar
							.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
								@Override
								public void onStopTrackingTouch(SeekBar seekBar) {
								}

								@Override
								public void onStartTrackingTouch(SeekBar seekBar) {
								}

								@Override
								public void onProgressChanged(SeekBar seekBar,
										int progress, boolean arg2) {
									font_text_view.setTextSize(progress);
									fontSize = progress;
								}
							});
					fontColor.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View v) {
							ColorPickerDialog dialog = new ColorPickerDialog(
									SimpleTextReaderActivity.this, "选择颜色",
									new OnColorChangedListener() {
										@Override
										public void colorChanged(int color) {
											textColor = color;
											fontColor.setTextColor(color);
										}
									});
							dialog.show();
						}
					});
					fontDialog.setTitle("字体设置");
					fontDialog.setView(fontView);
					fontDialog.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg0,
										int arg1) {
									saveFontStyle();
									initText();
								}
							});
					fontDialog.setNegativeButton("取消",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg0,
										int arg1) {
									fontSize = tempProgress;
									textColor = tempColor;
								}
							});
					fontDialog.show();
				}
					break;
				case 4: {
					// book mark
					final AlertDialog bookmarkDialog = new AlertDialog.Builder(
							SimpleTextReaderActivity.this).create();
					View bookmarkView = View.inflate(
							SimpleTextReaderActivity.this,
							R.layout.reader_bookmark, null);
					ListView bookmarkListView = (ListView) bookmarkView
							.findViewById(R.id.bookmarklist);
					DianDiReaderService ds = new DianDiReaderService();
					// load bookmark datas
					bookmarkList = ds.listBookmark(file.getName());
					final BookMarkLoadAdapter adapter = new BookMarkLoadAdapter();
					bookmarkListView.setAdapter(adapter);
					// load bookmark
					bookmarkListView
							.setOnItemClickListener(new OnItemClickListener() {
								@Override
								public void onItemClick(AdapterView<?> arg0,
										View view, int position, long arg3) {
									bookmarkDialog.dismiss();
									bm = bookmarkList.get(position);
									totalSkipChars = bm.getSkipBytes();
									initText();
									saveTotalSkipBytes();
								}
							});
					// delete bookmark
					bookmarkListView
							.setOnItemLongClickListener(new OnItemLongClickListener() {
								@Override
								public boolean onItemLongClick(
										AdapterView<?> arg0, View view,
										int position, long arg3) {
									bm = bookmarkList.get(position);
									AlertDialog.Builder builder = new AlertDialog.Builder(
											SimpleTextReaderActivity.this);
									builder.setTitle("提示");
									builder.setMessage("删除该书签?");
									builder
											.setPositiveButton(
													"确定",
													new DialogInterface.OnClickListener() {
														@Override
														public void onClick(
																DialogInterface arg0,
																int arg1) {
															DianDiReaderService drs = new DianDiReaderService();
															drs
																	.deleteBookmark(bm
																			.getId());
															bookmarkList
																	.remove(bm);
															adapter
																	.notifyDataSetChanged();
														}
													});
									builder.setNegativeButton("取消", null);
									builder.show();
									return false;
								}
							});
					bookmarkDialog.setView(bookmarkView);
					bookmarkDialog.show();

				}
					break;
				case 5: {
					int type = preferences.getInt(DiandiReaderVO.BACKGROUNDIMAGE, DiandiReaderVO.BACKGROUNDIMAGE_DAY);
					Editor edit = preferences.edit();
					if(type==DiandiReaderVO.BACKGROUNDIMAGE_DAY){
						readerEditText.setBackgroundResource(R.drawable.reader_main_background);
						edit.putInt(DiandiReaderVO.BACKGROUNDIMAGE, DiandiReaderVO.BACKGROUNDIMAGE_NIGHT);
					}else if(type==DiandiReaderVO.BACKGROUNDIMAGE_NIGHT){
						readerEditText.setBackgroundResource(R.drawable.reader_main_background_day);
						edit.putInt(DiandiReaderVO.BACKGROUNDIMAGE, DiandiReaderVO.BACKGROUNDIMAGE_DAY);
					}
					edit.commit();
				}
					break;
				case 6: {

				}
					break;
				case 7: {

				}
					break;
				}

			}

		});
	}

	// get system properties
	public void getDimensionOfView() {
		// 获得和系统相关的数据
		displayMetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
		screenWidth = displayMetrics.widthPixels;
		screenHeight = displayMetrics.heightPixels;
		// 获得状态栏高度
//		Rect rect = new Rect();
//		getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
		// 状态栏高度
//		int statusBarHeight = rect.top;
		// 标题栏高度
//		int titleBarHeight = 0;
		// 计算屏幕的可视区域
//		screenHeight = screenHeight - titleBarHeight - statusBarHeight;
		// 获得每页的最大字数
		maxPageBytes = maxPageBytes();
	}

	public void loadUserCfg() {
		totalSkipChars = readPositionPreference.getLong(file.getName(), 0);
		fileSize = readPositionPreference.getLong("T"+file.getName(), 0);
		fontSize = preferences.getFloat(DiandiReaderVO.FONTSIZE,
				DiandiReaderVO.FONTSIZE_DEFAULT);
		textColor = preferences.getInt(DiandiReaderVO.FONTCOLOR, Color.BLACK);
	}

	// get max words counts each page
	public int maxPageBytes() {
		countInColumns = (int) ((screenHeight - bookInfoLayout.getHeight()) / readerEditText
				.getLineHeight());// 字体大小*缩放倍数
		countInRows = (int) ((screenWidth - readerEditText.getPaddingLeft() - readerEditText
				.getPaddingRight()) / (fontSize * displayMetrics.scaledDensity));
		return countInColumns * countInRows;
	}

	public void initText() {
		// set display dimension
		readerEditText.setTextSize(fontSize);
		getDimensionOfView();
		bookName.setText(FileHelper.getFileWithoutSuffix(file.getName()));
		readerEditText.setTextColor(textColor);
		// remove the unread char
		readerEditText.setText(readNextPageData(totalSkipChars));
		setBookDisplayInfo();
	}

	// set previous page text
	public void setPrevPageText() {
		readerEditText.scrollTo(0, 0);
		readerEditText.setText(readPrevPageData(totalSkipChars));
		totalSkipChars = totalSkipChars - currentPageChars;
		if (totalSkipChars < 0) {
			totalSkipChars = 0;
		}
		if (totalSkipChars == 0) {
			Toast.makeText(SimpleTextReaderActivity.this, "第一页",
					Toast.LENGTH_SHORT).show();
		}
		setBookDisplayInfo();
		// update
		saveTotalSkipBytes();
	}

	// set next page data
	public void setNextPageText() {
		totalSkipChars = totalSkipChars + currentPageChars;
		String text = readNextPageData(totalSkipChars);
		if (readResult == -1) {
			totalSkipChars = totalSkipChars - currentPageChars;
			Toast.makeText(SimpleTextReaderActivity.this, "最后一页",
					Toast.LENGTH_SHORT).show();
			// remove the scroll
			handler.sendEmptyMessage(DiandiReaderVO.MESSAGE_SCROLLSTOP);
			return;
		}
		readerEditText.scrollTo(0, 0);
		readerEditText.setText(text);
		setBookDisplayInfo();
		//
		saveTotalSkipBytes();
	}

	// read next page data
	public String readNextPageData(long totalSkipChars) {

		char[] buffer = new char[maxPageBytes];
		if (codeType == null)
			codeType = FileHelper.getFileEncodeType(file);
		String str = "";
		try {
			reader = new InputStreamReader(new FileInputStream(file), codeType);
			reader.skip(totalSkipChars);
			readResult = reader.read(buffer, 0, maxPageBytes);
			// 计算本页需要显示多少
			calculateFixBytes(buffer);
			str = new String(buffer, 0, currentPageChars);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return str;
	}

	// 读取上一页数据
	public String readPrevPageData(long totalSkipChars) {

		char[] buffer = new char[maxPageBytes];
		if (codeType == null)
			codeType = FileHelper.getFileEncodeType(file);
		String str = "";
		try {
			reader = new InputStreamReader(new FileInputStream(file), codeType);
			// 如果已经跳过的字符数<每页的最大显示数，就从0开始读
			if (totalSkipChars - maxPageBytes <= 0) {
				reader.skip(0);
				readResult = reader.read(buffer, 0, maxPageBytes);
				calculateFixBytes(buffer);
				str = new String(buffer, 0, currentPageChars);
				return str;
			} else {
				reader.skip(totalSkipChars - maxPageBytes);
				readResult = reader.read(buffer, 0, maxPageBytes);
				// 计算本页需要显示多少
				calculateFixByteBackwords(buffer);
				str = new String(buffer, maxPageBytes - currentPageChars,
						currentPageChars);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return str;
	}

	// 计算本页能显示多少(计算换行)
	public void calculateFixBytes(char[] buffer) {
		int totalLines = 0;
		int newLineBytes = 0;// 新行的字符数
		int i = 0;
		int j = 1;
		for (i = 0; i < maxPageBytes; i++) {
			newLineBytes++;
			if (newLineBytes % countInRows == 0) {
				totalLines++;// 行数加1
			}
			if (buffer[i] == '\n') {
				totalLines++;
				newLineBytes = 0;
			}
			if (totalLines >= countInColumns) {
				break;
			}
			j++;
		}
		currentPageChars = j;
	}

	// 计算本页能显示的数据（倒序）
	public void calculateFixByteBackwords(char[] buffer) {
		int totalLines = 0;
		int newLineBytes = 0;// 新行的字符数
		int i = 0;
		for (i = maxPageBytes - 1; i >= 0; i--) {
			newLineBytes++;
			if (newLineBytes % countInRows == 0 || buffer[i] == '\n') {
				totalLines++;// 行数加1
				newLineBytes = 0;
			}
			if (totalLines >= countInColumns) {
				break;
			}
		}
		currentPageChars = maxPageBytes - i;
	}

	// set the bytes has skiped
	private void saveTotalSkipBytes() {
		Editor edit = readPositionPreference.edit();
		edit.putLong(file.getName(), totalSkipChars);
		edit.commit();
	}

	// save the font info
	private void saveFontStyle() {
		Editor edit = preferences.edit();
		edit.putFloat(DiandiReaderVO.FONTSIZE, fontSize);
		edit.putInt(DiandiReaderVO.FONTCOLOR, textColor);
		edit.commit();
	}

	// set book display info
	private void setBookDisplayInfo() {
		double tempSize = totalSkipChars + currentPageChars;
		if (tempSize >= fileSize) {
			tempSize = fileSize;
		}
		if(fileSize==0)
		fileSize = readPositionPreference.getLong("T"+file.getName(), 0);
		if(fileSize==0)
			return;
		pageCount.setText(format.format(tempSize * 100 / (double) fileSize)
				+ "%");
	}

	// handler the message
	class OptionHandler extends Handler {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case DiandiReaderVO.MESSAGE_SCROLLING: {
				setNextPageText();
				handler.sendEmptyMessageDelayed(
						DiandiReaderVO.MESSAGE_SCROLLING, 4000);
			}
				break;
			case DiandiReaderVO.MESSAGE_SCROLLSTOP: {
				handler.removeMessages(DiandiReaderVO.MESSAGE_SCROLLING);
			}
			}
		}

	}

	// ---------------------------------------comm
	// setting----------------------------------------
	// volumn control
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// ++
		if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
			setPrevPageText();
			return true;
		}
		if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
			setNextPageText();
			return true;
		}
		if (keyCode == KeyEvent.KEYCODE_BACK
				&& optionMenu.getVisibility() == View.VISIBLE) {
			optionMenu.setVisibility(View.GONE);
			return true;
		}
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			handler.sendEmptyMessage(DiandiReaderVO.MESSAGE_SCROLLSTOP);
			finish();
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 1, 0, "menu");
		// readerOptionView
		return super.onCreateOptionsMenu(menu);
	}

	//
	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {
		if (optionMenu.getVisibility() == View.VISIBLE)
			optionMenu.setVisibility(View.GONE);
		else
			optionMenu.setVisibility(View.VISIBLE);
		return false;
	}

	// bookmark load Adapter
	class BookMarkLoadAdapter extends BaseAdapter {
		@Override
		public View getView(int position, View view, ViewGroup parent) {
			BookMarkVO bm = bookmarkList.get(position);
			view = View.inflate(SimpleTextReaderActivity.this,
					R.layout.reader_bookmark_item, null);
			TextView bookmarkName = (TextView) view
					.findViewById(R.id.bookmarkname);
			bookmarkName.setText(bm.getBookmarkName());
			TextView createTime = (TextView) view.findViewById(R.id.createtime);
			createTime.setText(bm.getTime());
			TextView currentPosition = (TextView)view.findViewById(R.id.currentposition);
			currentPosition.setText(format.format((double)bm.getSkipBytes()*100/(double)fileSize)+"%");
			return view;
		}

		@Override
		public long getItemId(int position) {

			return position;
		}

		@Override
		public Object getItem(int position) {

			return bookmarkList.get(position);
		}

		@Override
		public int getCount() {

			return bookmarkList.size();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onPause() {
		super.onPause();
		try {
			if(reader!=null)
			reader.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
